package com.jiexin.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.jiexin.beans.PageQuery;
import com.jiexin.common.JsonData;
import com.jiexin.model.SysUser;
import com.jiexin.param.AclParam;
import com.jiexin.param.RoleParam;
import com.jiexin.service.*;
import com.jiexin.util.StringUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: Jay_Liu
 * @Description:
 * @Date: Created in 16:23 2018/4/20 0020
 * @Modified By:
 */
@Controller
@RequestMapping("/sys/role")
public class SysRoleController {

    @Resource
    SysRoleService sysRoleService;

    @Resource
    SysTreeService sysTreeService;

    @Resource
    SysRoleAclService sysRoleAclService;

    @Resource
    SysRoleUserService sysRoleUserService;

    @Resource
    SysUserService sysUserService;

    @RequestMapping("/role.page")
    public ModelAndView page() {
        return new ModelAndView("role");
    }

    @RequestMapping("/save.json")
    @ResponseBody
    //保存
    public JsonData saveAcl(RoleParam aclParam) {
        sysRoleService.save(aclParam);
        return JsonData.success();
    }

    @RequestMapping("/update.json")
    @ResponseBody
    //更新
    public JsonData updateAcl(RoleParam aclParam) {
        sysRoleService.update(aclParam);
        return JsonData.success();
    }

    @RequestMapping("/list.json")
    @ResponseBody
    //角色点分页数据
    public JsonData list() {
        return JsonData.success(sysRoleService.getAllRole());
    }

    @RequestMapping("/roleTree.json")
    @ResponseBody
    //当前角色的权限树
    public JsonData roleTree(@RequestParam("roleId") Integer roleId) {
        return JsonData.success(sysTreeService.roleTree(roleId));
    }

    @RequestMapping("/changeAcls.json")
    @ResponseBody
    //保存权限与角色的关系
    public JsonData changeAcl(@RequestParam("roleId") Integer roleId, @RequestParam(value = "aclIds", required = false, defaultValue = "") String aclIds) {
        List<Integer> aclIdList = StringUtil.splitToListInt(aclIds, ",");
        sysRoleAclService.changeRoleAcls(roleId, aclIdList);
        return JsonData.success();
    }

    @RequestMapping("/changeUsers.json")
    @ResponseBody
    //保存权限与角色的关系
    public JsonData changeUser(@RequestParam("roleId") Integer roleId, @RequestParam(value = "userIds", required = false, defaultValue = "") String userIds) {
        List<Integer> userIdList = StringUtil.splitToListInt(userIds, ",");
        sysRoleUserService.changeRoleUsers(roleId, userIdList);
        return JsonData.success();
    }

    @RequestMapping("/userTree.json")
    @ResponseBody
    //显示权限与用户的关系
    public JsonData userTree(@RequestParam("roleId") Integer roleId) {
        //当前角色下的关联的用户
        List<SysUser> selectedUserList = sysRoleUserService.getListByRoleId(roleId);
        //所有用户
        List<SysUser> allUserList = sysUserService.getAllUser();
        Set<Integer> selectedUserId = selectedUserList.stream().map(sysUser -> sysUser.getId()).collect(Collectors.toSet());
        //未关联的用户
        List<SysUser> unselectedUserList = Lists.newArrayList();
        for (SysUser sysUser : allUserList) {
            if (sysUser.getStatus() == 1 && !selectedUserId.contains(sysUser.getId())) {
                unselectedUserList.add(sysUser);
            }
        }
        //seletedUserList = seletedUserList.stream().filter(user -> user.getStatus()!=1 ).collect(Collectors.toList());
        Map<String, List<SysUser>> resultMap = Maps.newHashMap();
        resultMap.put("selected", selectedUserList);
        resultMap.put("unselected", unselectedUserList);
        return JsonData.success(resultMap);
    }
}
